﻿Public Class frmGopBan


    Public Event OnGopXong As EventHandler(Of EventArgs)
    Protected Sub SetGopXong(ban As Ban)

        RaiseEvent OnGopXong(ban, New EventArgs())

    End Sub

    Public Event OnGopXongDonHang As EventHandler(Of EventArgs)
    Protected Sub SetGopXongDonHang(ban As DonHang)

        RaiseEvent OnGopXongDonHang(ban, New EventArgs())

    End Sub
    Public BanChinh As Ban

    Private Sub frmGopBan_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
        LoadInput()
        tabBan.Text = String.Format("{0} gộp với", BanChinh.Ten)
    End Sub

    Public Sub LoadInput()
        Using db = New QlqaContext
            For Each o In db.Bans.Where(Function(x) x.DaSuDung And x.Id <> BanChinh.Id).ToList()
                Dim i = New ListViewItem(o.Ten, 0)
                i.Tag = o
                listViewBan.Items.Add(i)
            Next
        End Using

    End Sub

    Private Sub btnCapNhat_Click(sender As System.Object, e As System.EventArgs) Handles btnCapNhat.Click

        Dim dhChinh As DonHang
        Using db = New QlqaContext
            dhChinh = db.DonHangs.FirstOrDefault(Function(x) x.Id = BanChinh.IdDonHang)
            For Each listViewItem As ListViewItem In listViewBan.SelectedItems

                Dim b As Ban = listViewItem.Tag

                'Get DonHang
                Dim dh = db.DonHangs.ToList().Select(Function(x) New DonHang() With {.Id = x.Id, _
                                                        .Ban = x.Ban, _
                                                        .DaThanhToan = x.DaThanhToan, _
                                                        .ThanhTien = x.ThanhTien, _
                                                        .ChiTietDonHangs = db.ChiTietDonHangs.Where(Function(c) c.DonHang.Id = x.Id).ToList().Select(Function(c) New ChiTietDonHang() With {.Id = c.Id, .DonGia = c.DonGia, .DonHang = c.DonHang, .SanPham = db.SanPhams.ToList().FirstOrDefault(Function(s) s.Id = c.SanPham.Id), .SoLuong = c.SoLuong}).ToList()}).ToList() _
                                                .FirstOrDefault(Function(x) x.Id = b.IdDonHang)

                For Each chiTietDonHang In db.ChiTietDonHangs.Where(Function(x) x.DonHang.Id = dh.Id).ToList() _
                    .Select(Function(c) New ChiTietDonHang() With {.Id = c.Id, _
                                                                   .DonGia = c.DonGia, _
                                                                  .DonHang = db.DonHangs.ToList().FirstOrDefault(Function(s) s.Id = c.DonHang.Id), .SanPham = db.SanPhams.ToList().FirstOrDefault(Function(s) s.Id = c.SanPham.Id), .SoLuong = c.SoLuong}).ToList()
                    'Tìm trong ChiTietDonHangs xem có sản phẩm này chưa 
                    'Có thi update số lượng
                    'Không thì thêm mới

                    Dim cthdC = db.ChiTietDonHangs.FirstOrDefault(Function(x) x.SanPham.Id = chiTietDonHang.SanPham.Id And x.DonHang.Id = dhChinh.Id)

                    If cthdC IsNot Nothing Then
                        cthdC.SoLuong += chiTietDonHang.SoLuong
                    Else
                        cthdC = New ChiTietDonHang()
                        cthdC.DonHang = dhChinh
                        cthdC.SanPham = chiTietDonHang.SanPham
                        cthdC.SoLuong = chiTietDonHang.SoLuong
                        cthdC.DonGia = chiTietDonHang.DonGia
                        db.ChiTietDonHangs.Add(cthdC)

                    End If

                    db.ChiTietDonHangs.Remove(db.ChiTietDonHangs.FirstOrDefault(Function(x) x.Id = chiTietDonHang.Id))

                    db.DonHangs.Remove(db.DonHangs.FirstOrDefault(Function(x) x.Id = chiTietDonHang.DonHang.Id))

                    listViewItem.Remove()
                Next

                Dim bs = db.Bans.FirstOrDefault(Function(x) x.Id = b.Id)
                bs.DaSuDung = False
                bs.IdDonHang = 0
                db.SaveChanges()
                SetGopXong(bs)
            Next

            dhChinh.ThanhTien = dhChinh.ChiTietDonHangs.Sum(Function(x) x.DonGia * x.SoLuong)
            db.SaveChanges()
            SetGopXongDonHang(dhChinh)
        End Using
        Me.Close()

    End Sub
End Class